#define _CRT_SECURE_NO_WARNINGS 1
#pragma warning(disable:6031)



//https://leetcode.cn/problems/top-k-frequent-words/description/
class Solution {
    class Com
    {
    public:
        bool operator()(pair<string, int>& p1, pair<string, int>& p2)
        {
            if (p1.second != p2.second)
            {
                return p1.second < p2.second;
            }
            else
            {
                return p1.first > p2.first;
            }
        }
    };
public:
    vector<string> topKFrequent(vector<string>& words, int k) {


        vector<string> ret;
        map<string, int> m;


        for (auto e : words)
        {
            m[e]++;
        }

        priority_queue<pair<string, int>, vector<pair<string, int>>, Com> pq;


        for (auto e : m)
        {
            pq.push(e);
        }


        while (k--)
        {
            ret.push_back(pq.top().first);
            pq.pop();
        }

        return ret;


    }
};